﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.ComponentModel;
#endregion

namespace TunnelsOfDeath
{
    partial class Game1 : Game
    {
        protected void DrawMapArea(GameTime gameTime)
        {
            //GraphicsDevice.Clear(new Color(68,27,11));
            foreach (ViewPosition viewposition in Globals.MyCameraManager)
            {
                viewposition.ClearMyArea(spriteBatch);
                int startX = ((int)(viewposition.Position.X / Globals.OBJECTAREA)) * -1;
                int startY = ((int)(viewposition.Position.Y / Globals.OBJECTAREA)) * -1;

                if (startX < 0)
                    startX = 0;
                if (startY < 0)
                    startY = 0;
                int stopX = startX + 22;
                int stopY = startY + 12;

                if (stopX > Globals.Map.GetLength(0) - 1)
                    stopX = Globals.Map.GetLength(0);
                if (stopY > Globals.Map.GetLength(1) - 1)
                    stopY = Globals.Map.GetLength(1);

                for (int i = startX; i < stopX; i++)
                {
                    for (int u = startY; u < stopY; u++)
                    {

                        DrawBackground(i, u, viewposition);

                        if (Globals.Map[i, u] != null)
                        {
                            Globals.Map[i, u].Draw(spriteBatch, viewposition);
                        }
                    }
                }
            }
            if (Globals.MyCameraManager.SeparatorDisplay())
            {
                spriteBatch.Draw(Globals.ScreenCleaner, new Rectangle((int)((Globals.ScreenX / 2) - 1 * Globals.ScreenRatio), 0, (int)(2 * Globals.ScreenRatio), Globals.ScreenY), Color.White);
            }

        }

        private void DrawBackground(int x, int y, ViewPosition viewPosition)
        {
            Rectangle sourceRectangle = new Rectangle(0, 0, (int)Globals.OBJECTAREA, (int)Globals.OBJECTAREA);
            Vector2 nonScaledPosition = new Vector2(x, y) * Globals.OBJECTAREA + viewPosition.Position;
            if (nonScaledPosition.X < 0)
            {
                sourceRectangle.X += (int)Math.Abs(nonScaledPosition.X);
                nonScaledPosition.X += (int)Math.Abs(nonScaledPosition.X);
            }
            if (nonScaledPosition.Y < 0)
            {
                sourceRectangle.Y += (int)Math.Abs(nonScaledPosition.Y);
                nonScaledPosition.Y += (int)Math.Abs(nonScaledPosition.Y);
            }
            Vector2 realPosition = nonScaledPosition * Globals.ScreenRatio;

            spriteBatch.Draw(Globals.Textures[9], realPosition, sourceRectangle, Color.White, .0f, new Vector2(0, 0), Globals.ScreenRatio, SpriteEffects.None, 1f);
        }

    }
}
